package com.example.broadcastreceiver;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private TextView tvContent;
    private int num = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        // 动态注册系统广播
        registerSystemBroadcast();

        // 动态注册自定义广播
        registerNormal();

        // 动态注册有序广播
        registerOrder();
    }

    private void initView() {
        tvContent = findViewById(R.id.tv_text);

        // 标准广播：没有顺序，同时接收
        Button btnNormal = findViewById(R.id.btn_normal);
        btnNormal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num++;
                Intent intent = new Intent("com.example.broadcastreceiver.MY_NORMAL_BROADCAST");
                intent.putExtra("broadcast_type", "普通广播");
                intent.putExtra("msg", "这是普通广播第" + num + "次发送");
                sendBroadcast(intent);
            }
        });

        // 有序广播：按优先级顺序接收
        Button btnOrder = findViewById(R.id.btn_order);
        btnOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num++;
                Intent intent = new Intent("com.example.broadcastreceiver.MY_ORDERED_BROADCAST");
                intent.putExtra("broadcast_type", "有序广播");
                intent.putExtra("msg", "这是有序广播第" + num + "次发送");
                sendOrderedBroadcast(intent, null);
            }
        });
    }

    // 系统广播接收器
    private AirplaneReceiver airplaneReceiver;
    private NetworkChangeReceiver networkChangeReceiver;
    private void registerSystemBroadcast() {
        // 网络变化广播器
        networkChangeReceiver = new NetworkChangeReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
        registerReceiver(networkChangeReceiver, filter);

        // 飞行模式切换广播器
        airplaneReceiver = new AirplaneReceiver();
        filter = new IntentFilter();
        filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        registerReceiver(airplaneReceiver, filter);
    }

    // 自定义广播接收器
    private MyNormalReceiver myNormalReceiver;
    private void registerNormal() {
        // 自定义广播接收器注册
        myNormalReceiver = new MyNormalReceiver(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.example.broadcastreceiver.MY_NORMAL_BROADCAST");
        registerReceiver(myNormalReceiver, filter);
    }

    private MyOrderReceiver1 myOrderReceiver1;
    private MyOrderReceiver2 myOrderReceiver2;
    private MyOrderReceiver3 myOrderReceiver3;
    private void registerOrder() {
        myOrderReceiver1 = new MyOrderReceiver1();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.example.broadcastreceiver.MY_ORDERED_BROADCAST");
        filter.setPriority(10);
        registerReceiver(myOrderReceiver1, filter);

        myOrderReceiver2 = new MyOrderReceiver2();
        filter = new IntentFilter();
        filter.addAction("com.example.broadcastreceiver.MY_ORDERED_BROADCAST");
        filter.setPriority(20);
        registerReceiver(myOrderReceiver2, filter);

        myOrderReceiver3 = new MyOrderReceiver3();
        filter = new IntentFilter();
        filter.addAction("com.example.broadcastreceiver.MY_ORDERED_BROADCAST");
        filter.setPriority(12);
        registerReceiver(myOrderReceiver3, filter);
    }

    private void unregisterOrder() {
        unregisterReceiver(myOrderReceiver1);
        unregisterReceiver(myOrderReceiver2);
        unregisterReceiver(myOrderReceiver3);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(airplaneReceiver);
        unregisterReceiver(networkChangeReceiver);

        unregisterReceiver(myNormalReceiver);

        unregisterOrder();
    }

    public void setContent(String result) {
        this.tvContent.setText(result);
    }
}